package Shapes;

import processing.core.*;
import processing.xml.*;


import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;


/**
 * @author chen
 * @see multi-selections, which controls conditions, result in the change of final impact.
 */


public class parallelCoordinates extends shape {

	// Requires Scrollbar Class
	
	public ArrayList<Scrollbar> bars = new ArrayList<Scrollbar>(); // we suppose the output is only one

//	PImage img;
	
	
	  public parallelCoordinates(PApplet p) {
			super(p);
			// TODO Auto-generated constructor stub
		}	  

	public void setup() {
		
		parent.noStroke();
		// Inputs: x, y, width, height, minVal, maxVal
		for(int i = 0; i< 4; i++){
			Scrollbar bar = new Scrollbar(1000, 200+30*i, 150, 5, -200.0f, 0.0f);
			bars.add(bar);		
		}

	//	img = parent.loadImage("landscape.jpg");
	}

	public void update(){
		bars.clear();
	}
	
	public void draw() {
		
		for(int i = 0; i <bars.size() ; i++)
		{
			// image(img, x, 0);
			bars.get(i).update(parent.mouseX, parent.mouseY);
			bars.get(i).display();
			
			if(i!=0)
			{
			//	parent.fill(0);
				parent.line(bars.get(i-1).pos,bars.get(i-1).y,bars.get(i).pos,bars.get(i).y);
		//		parent.fill(255);
			}
		}
		
	}

	public void mousePressed() {
		for(int i = 0; i <bars.size(); i++)
		{
			bars.get(i).press(parent.mouseX, parent.mouseY);
		}
	}

	public void mouseReleased() {
		for(int i = 0; i <bars.size(); i++)
		{
			bars.get(i).release();
		}
	}

	class Scrollbar {
		int x, y; // The x- and y-coordinates
		float sw, sh; // Width and height of scrollbar
		float pos; // Position of thumb
		float posMin, posMax; // Max and min values of thumb
		boolean rollover; // True when the mouse is over
		boolean locked; // True when its the active scrollbar
		float minVal, maxVal; // Min and max values for the thumb

		Scrollbar(int xp, int yp, int w, int h, float miv, float mav) {
			x = xp;
			y = yp;
			sw = w;
			sh = h;
			minVal = miv;
			maxVal = mav;
			pos = x + sw / 2 - sh / 2;
			posMin = x;
			posMax = x + sw - sh;
		}

		// Updates the over boolean and the position of the thumb
		public void update(int mx, int my) {
			if (over(mx, my) == true) {
				rollover = true;
			} else {
				rollover = false;
			}
			if (locked == true) {
				pos = parent.constrain(mx - sh / 2, posMin, posMax);
			}
		}

		// Locks the thumb so the mouse can move off and still update
		public void press(int mx, int my) {
			if (rollover == true) {
				locked = true;
			} else {
				locked = false;
			}
		}

		// Resets the scrollbar to neutral
		public void release() {
			locked = false;
		}

		// Returns true if the cursor is over the scrollbar
		public boolean over(int mx, int my) {
			if ((mx > x) && (mx < x + sw) && (my > y) && (my < y + sh)) {
				return true;
			} else {
				return false;
			}
		}

		// Draws the scrollbar to the screen
		public void display() {
			parent.fill(255);
			parent.rect(x, y, sw, sh);
			if ((rollover == true) || (locked == true)) {
				parent.fill(0);
			} else {
				parent.fill(102);
			}
			parent.rect(pos, y, sh, sh);
		}

		// Returns the current value of the thumb
		public float getPos() {
			float scalar = sw / (sw - sh);
			float ratio = (pos - x) * scalar;
			float offset = minVal + (ratio / sw * (maxVal - minVal));
			return offset;
		}
	}

}
